<?php
namespace app\index\controller;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class GoodList{
    public function goodList()
    {
//        if(Request::isGet()){
//            $key = input('keywords');
//            $sql=Db::table('ecm_goods')->where('name','like',"%{$key}%")->select();
//            $sql2=Db::table('ecm_category')->select();
//            View::assign('sql2',$sql2);
//            View::assign('sql',$sql);
//        }else{
//            $sql=Db::table('ecm_goods')->select();
//            $sql2=Db::table('ecm_category')->select();
//            View::assign('sql2',$sql2);
//            View::assign('sql',$sql);
//        }


//        ====================================   查询字段
        $where=[];
        $key = input('keywords');
        if(isset($key) && !empty($key)){
            $where=[
                ['name','like',"%{$key}%"]
            ];
        }
        View::assign('keys',$key);



//        ====================================   热门
        $isshow = input('is_show');
        echo $isshow;
        if(isset($isshow) && !empty($isshow)){
            $where=[
                ['is_show','=',$isshow]
            ];
        }
        View::assign('isshow',$isshow);

//        ====================================   推荐
        $tj = input('tj');
        if(isset($tj) && !empty($tj)){
            $where=[
                ['is_hot','=',$tj]
            ];
        }
        View::assign('tj',$tj);


//        ====================================   上架
        $get=Request::get();
            if(isset($get['sj']) && count($get['sj'])==1){
                $where=[
                    ['is_down','in',$get['sj']]
                ];
        }


        if(isset($get['cid']) && $get['cid'] != ""){$where=[['cid','=',$get['cid']]];};

//        ===============金额

        if(isset($get['jin']) && $get['jin'] <> "" && isset($get['jin1']) && $get['jin1'] <> ""){$where=[['price','between',"{$get['jin']},{$get['jin1']}"]];};

//        ===============时间
        if(isset($get['sj1']) && $get['sj1'] <> "" && isset($get['sj2']) && $get['sj2'] <> ""){$where=[['addtime','between time',"{$get['sj1']},{$get['sj2']}"]];};



        View::assign($get);



        $sql=Db::table('ecm_goods')->where($where)->select();
        $sql2=Db::table('ecm_category')->select();
        View::assign('sql2',$sql2);
        View::assign('sql',$sql);
        $a=Db::name('category')->where('parentid','=','=')->select();
        View::assign('a',$a);
        $b=Db::name('category')->select();
        View::assign('b',$b);

        return View();
    }

    public function list()
    {
        //链式操作
        //起别名  1
        Db::table('ecm_goods g')->select();
        //起别名2
        Db::table('ecm_goods')->alias('g')->select();
        //起别名3
        Db::table(['ecm_goods'=>'g'])->select();
        //起别名4
        Db::table('ecm_goods')->alias([
            'ecm_goods'=>'g',
            'ecm_category'=>'c'
        ])->select();


        //where('字段','表达式','条件')    where(数组)


        //join   连表操作join('链接表名','条件','链接方式')
//        $join = Db::table('ecm_order o')
//            ->join('ecm_user u','o.uid=u.id','left')
//            ->select();

//        echo '<pre>';
//        print_r($join);
//        echo '</pre>';

        //field  字段查询  field(true)查询所有字段   withoutField('id')  不查询字段
//        $fin = Db::name('user')->field('uname u,id i')->select();
        $fin = Db::name('user')
//            ->field(true)                  //查询所有字段
            //->withoutField('id')           //不查询字段
            ->field('md5(id) id')       //使用函数
            ->select();
//        echo '<pre>';
//        print_r($fin);
//        echo '</pre>';


        //order  排序操作  相同时间id正序  逗号排序id倒叙
        //$order = Db::name('user')->order('addtime desc,id desc')->select();

        //$order = Db::name('user')->order(['addtime'=>'desc','id'=>'desc'])->select();

        //============================   随机排序
        $order = Db::name('user')->orderRand()->select();
        echo '<pre>';
        print_r($order);
        echo '</pre>';


        //limit   page   limit('索引位置','长度')   page('页码数','每页多少条')
        $page = Db::name('user')->page(0,10)->select();


        //group    分组
        $gr = Db::name('user')->group('addtime')
            ->having('count(addtime)>1')
            ->select();
        echo '<pre>';
        print_r($gr);
        echo '</pre>';

    }
}